package com.suntop.carousel.qlmediaplay.tools;

import android.annotation.SuppressLint;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

import wseemann.media.FFmpegMediaMetadataRetriever;
@SuppressLint("NewApi")
public class VideoFrameTool {
    private static VideoFrameTool instance;

    public static VideoFrameTool getInstance() {
        if (instance == null) {
            instance = new VideoFrameTool();
        }
        return instance;
    }

    /**
     * 获取网络视频第一帧
     *
     * @param videoUrl
     * @return
     */

    public void loadFFmengFirst(String videoUrl, String savePath,
                                int time, OnLoadVideoImageListener loadVideoImageListener) {
        VideoImageTask task = new VideoImageTask(time, loadVideoImageListener);
        task.execute(videoUrl, savePath);
    }
    public void loadFFmengFirst(AssetFileDescriptor fileDescriptor, String savePath,
                                int time, OnLoadVideoImageListener loadVideoImageListener) {
        VideoImageTask task = new VideoImageTask(time, loadVideoImageListener);
        task.execute(fileDescriptor, savePath);
    }


    public class VideoImageTask extends AsyncTask<Object, Integer, File> {
        private int time = 0;
        private OnLoadVideoImageListener loadVideoImageListener;

        public VideoImageTask(int time, OnLoadVideoImageListener loadVideoImageListener) {
            this.time = time * 1000000;
            this.loadVideoImageListener = loadVideoImageListener;
        }

        @Override
        protected File doInBackground(Object... strings) {
            Object o = strings[0];
            String path = (String) strings[1];
            File file = null;
            String url="";
            if (o instanceof AssetFileDescriptor){
                url = ((AssetFileDescriptor) o).getFileDescriptor().toString();
            }else if (o instanceof String){
                 url= (String) o;
            }
            if (TextUtils.isEmpty(url)){
                return file;
            }
            String s = takeFileName(url);
            file = new File(path, s);
            if (file.exists() && file.length() > 20) {
                return file;
            }
            FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
            try {
                if (o instanceof AssetFileDescriptor){
                    retriever.setDataSource(((AssetFileDescriptor) o).getFileDescriptor());
                }else {
                    retriever.setDataSource(url);
                }
                Bitmap frameAtTime = retriever.getFrameAtTime(time);
                file = FileUnit.saveBitmapFile(frameAtTime, s, path);
            } catch (Exception exception) {
                exception.printStackTrace();
                Log.e("设置错误", exception + "");
            } finally {
                retriever.release();
            }
            return file;
        }

        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            if (file != null) {
                if (loadVideoImageListener != null) {
                    loadVideoImageListener.onLoadImage(file);
                }
            }
        }
    }

    private String takeFileName(String url) {
        String s = FileUnit.takeFileName(url);
        String suffix = FileUnit.getSuffix(url);
        if (!TextUtils.isEmpty(suffix)) {
            s = "Cache_" + s.replace(suffix, "jpg");
        } else {
            s = "Cache_" + s + ".jpg";
        }
        return s;
    }

    public interface OnLoadVideoImageListener {
        void onLoadImage(File file);
    }


}
